package net.zjitc.controller;


import net.zjitc.pojo.CategorySecond;
import net.zjitc.pojo.Product;
import net.zjitc.service.AdminProductService;
import net.zjitc.service.CategorySecondService;
import net.zjitc.service.impl.AdminProductServiceImpl;
import net.zjitc.service.impl.CategorySecondSerImpl;
import net.zjitc.utils.CommonUtils;
import net.zjitc.utils.PageBean;
import net.zjitc.utils.dispacher.URLMapping;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.List;


/**
 * Created by Administrator on 2017/11/22.
 */
public class AdminProduct {
    AdminProductService productService=new AdminProductServiceImpl();
    CategorySecondService secondService=new CategorySecondSerImpl();
    //商品管理显示
    @URLMapping(url = "/adminProduct")
    public String adminProductList(HttpServletRequest request, HttpServletResponse response){
        int page=Integer.valueOf(request.getParameter("page"));
        int pageSize=12;
        PageBean<Product> pageBean=productService.findPage(page,pageSize);
        request.setAttribute("p",pageBean);
        return "/admin/product/list.jsp";
    }
    //添加页面显示
    @URLMapping(url = "/addUIProduct")
    public String addUIProduct(HttpServletRequest request, HttpServletResponse response){
        List<CategorySecond> list=secondService.findAll();
        request.setAttribute("clist",list);
        return "/admin/product/add.jsp";
    }
    //添加
    @URLMapping(url = "/addProduct")
    public String addProduct(HttpServletRequest request, HttpServletResponse response){
        //创建工厂
        DiskFileItemFactory factory=new DiskFileItemFactory();
        //解析器
        ServletFileUpload fileUpload =new ServletFileUpload(factory);
        fileUpload.setHeaderEncoding("utf-8");
        Product product=new Product();
        CategorySecond categorySecond=new CategorySecond();
        //解析
        try {
            List<FileItem> fileItems=fileUpload.parseRequest(request);
            for (FileItem item:fileItems){
                //处理表单普通数据项
                if (item.isFormField()){
                    if ("pname".equals(item.getFieldName())){
                        product.setPname(item.getString("utf-8"));
                    }
                    if ("shop_price".equals(item.getFieldName())){
                        product.setShop_price(Double.valueOf(item.getString()));
                    }
                    if ("market_price".equals(item.getFieldName())){
                        product.setMarket_price(Double.valueOf(item.getString()));
                    }
                    if ("is_hot".equals(item.getFieldName())){
                        product.setIs_hot(Integer.valueOf(item.getString()));
                    }
                    if ("pdesc".equals(item.getFieldName())){
                        product.setPdesc(item.getString("utf-8"));
                    }
                    if ("csid".equals(item.getFieldName())){
                        categorySecond.setCsid(Integer.valueOf(item.getString()));
                        product.setCategorySecond(categorySecond);
                    }
                }else {
                    InputStream inputStream=item.getInputStream();
                    String filename=item.getName();
                    filename= CommonUtils.uuid()+filename;
                    String path=request.getServletContext().getRealPath("/products/5");
                    File dir=new File(path);
                    if (!dir.exists()){
                        dir.mkdirs();
                    }
                    File file=new File(dir,filename);
                    item.write(file);

                    String imgPath="products/5/"+filename;
                    product.setImage(imgPath);

                }

            }
            product.setPdate(new Date());
            int count=productService.addProduct(product);
            System.out.println(product);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Redirect:"+request.getContextPath()+"/addUIProduct";
    }
    //回显
    @URLMapping(url = "/editUIProduct")
    public String editUIProduct(HttpServletRequest request, HttpServletResponse response){
        String pid=request.getParameter("pid");
        Product product=productService.findByPid(pid);
        request.setAttribute("pr",product);
        List<CategorySecond> list=secondService.findAll();
        request.setAttribute("clist",list);
        return "/admin/product/edit.jsp";
    }
    //编辑
    @URLMapping(url = "/editProduct")
    public String editProduct(HttpServletRequest request, HttpServletResponse response){
        //创建工厂
        DiskFileItemFactory factory=new DiskFileItemFactory();
        //解析器
        ServletFileUpload fileUpload =new ServletFileUpload(factory);
        fileUpload.setHeaderEncoding("utf-8");
        Product product=new Product();
        CategorySecond categorySecond=new CategorySecond();
        //解析
        try {
            List<FileItem> fileItems=fileUpload.parseRequest(request);
            for (FileItem item:fileItems){
                //处理表单普通数据项
                if (item.isFormField()){
                    if ("pid".equals(item.getFieldName())){
                        product.setPid(Integer.valueOf(item.getString()));
                    }
                    if ("pname".equals(item.getFieldName())){
                        product.setPname(item.getString("utf-8"));
                    }
                    if ("shop_price".equals(item.getFieldName())){
                        product.setShop_price(Double.valueOf(item.getString()));
                    }
                    if ("market_price".equals(item.getFieldName())){
                        product.setMarket_price(Double.valueOf(item.getString()));
                    }
                    if ("is_hot".equals(item.getFieldName())){
                        product.setIs_hot(Integer.valueOf(item.getString()));
                    }
                    if ("pdesc".equals(item.getFieldName())){
                        product.setPdesc(item.getString("utf-8"));
                    }
                    if ("csid".equals(item.getFieldName())){
                        categorySecond.setCsid(Integer.valueOf(item.getString()));
                        product.setCategorySecond(categorySecond);
                    }
                }else {
                    InputStream inputStream=item.getInputStream();
                    String filename=item.getName();
                    filename= CommonUtils.uuid()+filename;
                    String path=request.getServletContext().getRealPath("/products/5");
                    //System.out.println(path);
                    File dir=new File(path);
                    if (!dir.exists()){
                        dir.mkdirs();
                    }
                    File file=new File(dir,filename);
                    item.write(file);

                    String imgPath="products/5/"+filename;
                    product.setImage(imgPath);

                }

            }
            product.setPdate(new Date());
            int count=productService.editProduct(product);
            //System.out.println(product);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Redirect:"+request.getContextPath()+"/adminProduct?page=1";
    }
    //删除
    @URLMapping(url = "/deleteProduct")
    public String deleteProduct (HttpServletRequest request, HttpServletResponse response){
        String pid=request.getParameter("pid");
        int count=productService.deleteProduct(pid);
        return "Redirect:"+request.getContextPath()+"/adminProduct?page=1";
    }
}
